﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class ProductDAO :IProduct
    {
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Product");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Product");
            strSql.Append(" where code=@code");
            SqlParameter[] parameters = {
					new SqlParameter("@code", SqlDbType.VarChar,20)
			};
            parameters[0].Value = code;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.ProductModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Product(");
            strSql.Append("Code,Name,Image,Remark,Price,Discount,Current_Price,Quantity,Dic_ProductType,CreateDate,Creator,Last_Update_Date,IsRecommend,Sale_Sort,Sale)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Name,@Image,@Remark,@Price,@Discount,@Current_Price,@Quantity,@Dic_ProductType,@CreateDate,@Creator,@Last_Update_Date,@IsRecommend,@Sale_Sort,@Sale)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,200),
					new SqlParameter("@Image", SqlDbType.VarChar,255),
					new SqlParameter("@Remark", SqlDbType.Text),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Discount", SqlDbType.Decimal,9),
					new SqlParameter("@Current_Price", SqlDbType.Decimal,9),
					new SqlParameter("@Quantity", SqlDbType.Int,4),
					new SqlParameter("@Dic_ProductType", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@Last_Update_Date", SqlDbType.DateTime),
					new SqlParameter("@IsRecommend", SqlDbType.Int,4),
					new SqlParameter("@Sale_Sort", SqlDbType.Int,4),
					new SqlParameter("@Sale", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Image;
            parameters[3].Value = model.Remark;
            parameters[4].Value = model.Price;
            parameters[5].Value = model.Discount;
            parameters[6].Value = model.Current_Price;
            parameters[7].Value = model.Quantity;
            parameters[8].Value = model.Dic_ProductType;
            parameters[9].Value = model.CreateDate;
            parameters[10].Value = model.Creator;
            parameters[11].Value = model.Last_Update_Date;
            parameters[12].Value = model.IsRecommend;
            parameters[13].Value = model.Sale_Sort;
            parameters[14].Value = model.Sale;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.ProductModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Product set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("Image=@Image,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("Price=@Price,");
            strSql.Append("Discount=@Discount,");
            strSql.Append("Current_Price=@Current_Price,");
            strSql.Append("Quantity=@Quantity,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("Last_Update_Date=@Last_Update_Date,");
            strSql.Append("IsRecommend=@IsRecommend,");
            strSql.Append("Sale_Sort=@Sale_Sort,");
            strSql.Append("Sale=@Sale");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,200),
					new SqlParameter("@Image", SqlDbType.VarChar,255),
					new SqlParameter("@Remark", SqlDbType.Text),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Discount", SqlDbType.Decimal,9),
					new SqlParameter("@Current_Price", SqlDbType.Decimal,9),
					new SqlParameter("@Quantity", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@Last_Update_Date", SqlDbType.DateTime),
					new SqlParameter("@IsRecommend", SqlDbType.Int,4),
					new SqlParameter("@Sale_Sort", SqlDbType.Int,4),
					new SqlParameter("@Sale", SqlDbType.Int,4),
					new SqlParameter("@AID", SqlDbType.Int,4),
					new SqlParameter("@Dic_ProductType", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Image;
            parameters[3].Value = model.Remark;
            parameters[4].Value = model.Price;
            parameters[5].Value = model.Discount;
            parameters[6].Value = model.Current_Price;
            parameters[7].Value = model.Quantity;
            parameters[8].Value = model.CreateDate;
            parameters[9].Value = model.Creator;
            parameters[10].Value = model.Last_Update_Date;
            parameters[11].Value = model.IsRecommend;
            parameters[12].Value = model.Sale_Sort;
            parameters[13].Value = model.Sale;
            parameters[14].Value = model.AID;
            parameters[15].Value = model.Dic_ProductType;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Product ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Product ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.ProductModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,Name,Image,Remark,Price,Discount,Current_Price,Quantity,Dic_ProductType,CreateDate,Creator,Last_Update_Date,IsRecommend,Sale_Sort,Sale from T_Product ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;
            parameters[0].Value = AID;

            Models.ProductModel model = new Models.ProductModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.ProductModel ConvertToModel(DataRow row)
        {
            Models.ProductModel model = new Models.ProductModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Image"] != null)
                {
                    model.Image = row["Image"].ToString();
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["Price"] != null && row["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(row["Price"].ToString());
                }
                if (row["Dic_ProductType"] != null && row["Dic_ProductType"].ToString() != "")
                {
                    model.Dic_ProductType = int.Parse(row["Dic_ProductType"].ToString());
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (row["Creator"] != null)
                {
                    model.Creator = row["Creator"].ToString();
                }
                if (row.Table.Columns.Contains("ProductType"))
                {
                    if (row["ProductType"] != null)
                    {
                        model.ProductType = row["ProductType"].ToString();
                    }
                }

                if (row["Last_Update_Date"] != null && row["Last_Update_Date"].ToString() != "")
                {
                    model.Last_Update_Date = DateTime.Parse(row["Last_Update_Date"].ToString());
                }

                if(row["Discount"]!=null && row["Discount"].ToString()!="")
				{
					model.Discount=decimal.Parse(row["Discount"].ToString());
				}
				if(row["Current_Price"]!=null && row["Current_Price"].ToString()!="")
				{
					model.Current_Price=decimal.Parse(row["Current_Price"].ToString());
				}
                if (row["Quantity"] != null && row["Quantity"].ToString() != "")
                {
                    model.Quantity = int.Parse(row["Quantity"].ToString());
                }
                if (row["IsRecommend"] != null && row["IsRecommend"].ToString() != "")
                {
                    model.IsRecommend = int.Parse(row["IsRecommend"].ToString());
                }
                if (row["Sale_Sort"] != null && row["Sale_Sort"].ToString() != "")
                {
                    model.Sale_Sort = int.Parse(row["Sale_Sort"].ToString());
                }
                if (row["Sale"] != null && row["Sale"].ToString() != "")
                {
                    model.Sale = int.Parse(row["Sale"].ToString());
                }
            }
            return model;
        }

        protected List<Models.ProductModel> ConvertToList(DataSet ds)
        {
            List<Models.ProductModel> list = new List<Models.ProductModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.ProductModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.ProductModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select p.AID,p.Code,p.Name,p.Image,p.Remark,p.Price,p.Discount,p.Current_Price,p.Quantity,p.Dic_ProductType,d.Dic_Value as ProductType,p.CreateDate,p.Creator ,Last_Update_Date,IsRecommend,Sale_Sort,Sale ");
            strSql.Append(" FROM T_Product p inner join T_Dictionary d on p.Dic_ProductType=d.AID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.ProductModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append("p.AID,p.Code,p.Name,p.Image,p.Remark,p.Price,p.Discount,p.Current_Price,p.Quantity,p.Dic_ProductType,d.Dic_Value as ProductType,p.CreateDate,p.Creator ,Last_Update_Date,IsRecommend,Sale_Sort,Sale ");
            strSql.Append(" FROM T_Product p inner join T_Dictionary d on p.Dic_ProductType=d.AID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Product ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.ProductModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Product T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Models.ProductModel> GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Product";
            parameters[1].Value = "AID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            DataSet ds = SqlHelper.RunProcedure("UP_GetRecordByPage", parameters, "ds");
            return ConvertToList(ds);
        }
    }
}
